/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#ifndef __SEARCHTHREAD_H__
#define __SEARCHTHREAD_H__

#include <QThread>
#include <QObject>
#include <QTreeWidget>

#include "datamodel.h"

class SearchResultItem : public QObject
{
	Q_OBJECT
public:
	SearchResultItem(QString type, QString title, QString context, int pos, QTreeWidgetItem *node);

	QString stType;
	QString stTitle;
	QString stContext;
	int inPos;
	QTreeWidgetItem *dataNode;
};

class SearchThread : public QThread
{
	Q_OBJECT
public:
	SearchThread(QTreeWidget *tree, Model *model);
	
	bool isSearching();
	void initSearch(QList<SearchResultItem *> *results, QString searchTerm, bool wholeWord, bool caseSensitive, bool searchPage, bool searchCategory, bool searchTags);
	
	bool isSearchInPages();
	bool isSearchInCategories();
	
	void run();

signals:
	void searchFinished();
	
private:
	QTreeWidget *m_tree;
	Model *m_model;
	bool m_boSearching;
	QRegExp *m_regExp;
	
	QList<SearchResultItem *> *m_results;
	
	QString m_searchTerm;
	bool m_boWholeWord;
	bool m_boSearchInPages;
	bool m_boSearchInCategories;
	bool m_boSearchInTags;
	Qt::CaseSensitivity m_caseSensitive;
	
	void processNode(QTreeWidgetItem *item);
	bool match(QString source);	
};

#endif // __SEARCHTHREAD_H__
